<template>
    <el-dialog title="批量修改" width="600" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form"  label-width="100px" v-loading="loading">
      <el-form-item label="所属医院" prop="hospital">
        <el-select placeholder="请选择所属医院" class="w100" clearable v-model="form.affiliatedHospital">
							<el-option :key="item.value" :label="item.label" :value="item.value" v-for="item in affiliatedHospital" />
				</el-select>
      </el-form-item>
    </el-form>
    <!-- 提交信息 -->
    <template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="systemPostDialog">
import {useMessage} from '/@/hooks/message';
import { useI18n } from 'vue-i18n';
import { putObj } from '/@/api/doctor/doctor';
import { putbatchupdObj } from '/@/api/doctor/doctor';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const {t} = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 提交表单数据
const form = reactive({
  "doctorUid": 0,
  "doctorName": "",
  "doctorPhonenumber": "",
  "position": "",
  "affiliatedHospital": "",
  "permission": "",
  "department": "",
  batchForms: [] as any[]
});

const affiliatedHospital = ref([
  { value: '长顺县医疗集团中心医院', label: '长顺县医疗集团中心医院' },
  { value: '长顺县人民医院', label: '长顺县人民医院' },
  { value: '长顺现代民族医院', label: '长顺现代民族医院' }
]);


const openDialog = (obj: any | any[]) => {
  console.log('openDialog called with id:', obj); // 调试信息
  visible.value = true;

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 填充数据
  if (Array.isArray(obj)) {
    // 批量修改时，假设第一个选中的对象为基准
    form.batchForms = obj.map(item => ({
      doctorUid: item.doctorUid,
      doctorPhonenumber: item.doctorPhonenumber,
      doctorName: item.doctorName,
      position: item.position,
      department: item.department,
    }));
    /* useMessage().success('进入批量修改'); */
  } else {
    // 单个修改时
    form.doctorUid = obj.doctorUid;
    form.doctorPhonenumber = obj.doctorPhonenumber;
    form.doctorName = obj.doctorName;
    form.position = obj.position;
    form.department = obj.department;
  }
};



;

const onSubmit = async () => {
  try {
    loading.value = true;
    if (form.batchForms.length > 0) {
      // 批量提交
      /* useMessage().success('批量修改提交'); */
      form.batchForms.forEach(item =>(item["affiliatedHospital"]=form.affiliatedHospital))
      const res = await putbatchupdObj(form.batchForms);
      if (res.code != 1) {
      useMessage().success('批量修改成功');
      } else {
      useMessage().error('批量修改不成功');
      }
     console.log(form.batchForms)
    } else {
      // 单个提交
      const res = await putObj(form);
      if (res.code != 1) {
      useMessage().success('修改成功');
      } else {
      useMessage().error('修改不成功');
      }
    }

    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};



// 暴露变量
defineExpose({
  openDialog,
  form,
});
</script>
